package com.jianzhile.jobs.activities.person;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.utils.CommonUtil;
import com.jianzhile.jobs.utils.Const;
import com.jianzhile.jobs.utils.HttpUtil;
import com.jianzhile.jobs.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import co.lujun.androidtagview.TagContainerLayout;

/**
 * 完善个人信息
 */
public class PerfectInfoActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.content_perfect_info_mael_tv)
    TextView femaleTv;
    @Bind(R.id.content_perfect_info_age_tv)
    TextView ageTv;
    @Bind(R.id.content_perfect_info_job_tv)
    TextView jobTv;
    @Bind(R.id.vMasker)
    View vMasker;
    @Bind(R.id.content_perfect_info_name_et)
    public TextView nameTv;
    @Bind(R.id.content_perfect_info_job_tv)
    public TextView jobTv;
    @Bind(R.id.activity_perfect_info_job_ll)
    public LinearLayout joblL;
    @Bind(R.id.content_perfect_info_email_et)
    public TextView emailTv;
    @Bind(R.id.activity_perfect_info_hobby_fl)
    public LinearLayout hobbylL;
    @Bind(R.id.content_perfect_info_desc_tv)
    public TextView descriptTv;


    OptionsPickerView pickerView;
    TimePickerView agePv;

    private ArrayList<String> optionsItems = new ArrayList<>();



    @Bind(R.id.content_perfect_tag_container_layout)
    TagContainerLayout mTagContainerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfect_info);

        ButterKnife.bind(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setElevation(0);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDefaultDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PerfectInfoActivity.this.finish();
            }
        });
        ab.setTitle("");

        initFemaleOptions();
        initListener();

        List<String> list = Arrays.asList(getResources().getStringArray(R.array.hobby));
        mTagContainerLayout.setTags(list);

        initAgePicker();
    }

    private void initFemaleOptions(){

        pickerView = new OptionsPickerView(this);

        pickerView.setCancelable(true);
        pickerView.setTitle("选择性别");
        optionsItems.add("男");
        optionsItems.add("女");
        pickerView.setPicker(optionsItems);
        pickerView.setCyclic(false);
        pickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                femaleTv.setText(optionsItems.get(options1));
            }
        });
    }

    private void initAgePicker(){
        agePv  = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        agePv.setRange(calendar.get(Calendar.YEAR)-99, calendar.get(Calendar.YEAR));
        agePv.setTime(new Date());
        agePv.setCyclic(false);
        agePv.setCancelable(true);
        agePv.setTitle("出生日期");

        agePv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                ageTv.setText(CommonUtil.getAge(date));
            }
        });
    }

    private void initListener() {
        femaleTv.setOnClickListener(this);

        mTagContainerLayout.setOnClickListener(this);
        ageTv.setOnClickListener(this);
        jobTv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.content_perfect_info_mael_tv:
                pickerView.show();
                break;
            case R.id.content_choose_hobby_tag_container_layout:
                Intent intent = new Intent(PerfectInfoActivity.this, ChooseHobbyActivity.class);
                startActivity(intent);
                break;
            case R.id.content_perfect_info_age_tv:
                agePv.show();
                break;
            case R.id.content_perfect_info_job_tv:
                Intent jobIntent = new Intent(PerfectInfoActivity.this, ChooseJobActivity.class);
                startActivity(jobIntent);
                break;
        }
    }


    private void perfectInfo(){


        // 封装请求数据信息
        Map<String, String> params = new HashMap<>();
        params.put("", "");

        HttpUtil.getInstance().requestWithSecurity(this, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.optInt("code");
                    if (Const.status_1 == code) {

                    } else {
                        ToastUtils.showBottomToast(PerfectInfoActivity.this, jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtils.showBottomToast(PerfectInfoActivity.this, R.string.tips);
            }
        }, CommonUtil.getInstance().getParams(params), Const.PerfectInfo, true);
    }
}
